package com.myproject.sbp.order.interceptor;

import com.myproject.sbp.base.auth.AuthPermission;
import com.myproject.sbp.base.common.PermissionException;
import com.myproject.sbp.order.cache.RedisCacheImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@Component
public class WebAuthInterceptor implements HandlerInterceptor {

    @Autowired
    private RedisCacheImpl redisCache;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws PermissionException {
        if (handler instanceof HandlerMethod) {
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            RequestMapping requestMapping = handlerMethod.getMethodAnnotation(RequestMapping.class);
            if (requestMapping == null) {
                return true;
            }
            //todo
            redisCache.set("name", "test");
            String o = redisCache.getString("name");
            log.info("===================:{}", o);

            AuthPermission authPermission = handlerMethod.getMethodAnnotation(AuthPermission.class);
            if(null != authPermission){
                log.info("拦截");
            }
            if (requestMapping == null) {
                return true;
            }
        }
        return true;
    }

}
